
// pages/filedetail/filedetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    fileInfo: {},
    zan: true, //点赞
    pay_gold: false, //支付金币弹窗
    httpfile: null, //文件本地缓存地址

  },

  //下载文件
  downLoad: function () {
    var that = this
    if(getApp().globalData.userInfo.user_id==that.data.fileInfo.user_id){
      //复制下载链接到剪切板
      wx.setClipboardData({
        data: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/down_file?file_id=' + that.data.fileInfo.file_id,
        success: res => {
          wx.showToast({
            title: '复制成功',
            icon: 'success'
          })
        }
      })
    }else{
      if(getApp().globalData.userInfo.user_id!=undefined){
        that.setData({
          pay_gold: true,
        })
      }else{
        wx.showModal({
          title: '提示!',
          content: '请授权后再使用小程序',
          showCancel: false,
          success(res) { 
            wx.navigateTo({
              url: '/pages/login/login'
            })
          }
        })
      }
    }
    
  },


  //金币支付
  preventTouchMove: function () {},

  //取消支付
  cancel: function () {
    this.setData({
      pay_gold: false,
    })
  },

  //确定支付
  pay: function () {
    var that = this

    //获取当前时间戳
    var timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000;
    console.log(timestamp)

    if (parseInt(getApp().globalData.userInfo.gold_all_num) >= parseInt(that.data.fileInfo.gold_num) || parseInt(getApp().globalData.userInfo.user_id) == parseInt(that.data.fileInfo.user_id)) {
      that.setData({
        pay_gold: false,
      })

      //复制下载链接到剪切板
      wx.setClipboardData({
        data: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/down_file?file_id=' + that.data.fileInfo.file_id,
        success: res => {
          wx.showToast({
            title: '复制成功',
            icon: 'success'
          })

          //文件下载量+1
          wx.request({
            url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/add_down_num',
            method: "POST", //请求方式
            header: {
              'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
            },
            data: {
              file_id: parseInt(that.data.fileInfo.file_id),
              //后端完成：两个用户表的金币数加减
              user_id: parseInt(that.data.fileInfo.user_id),
              my_id: parseInt(getApp().globalData.userInfo.user_id),
              gold_num: parseInt(that.data.fileInfo.gold_num),
              move_time: timestamp,
              name1: that.data.fileInfo.user_name,
              name2: getApp().globalData.userInfo.user_name,
            },
            success(res) {
              console.log(res.data)
              if (parseInt(getApp().globalData.userInfo.user_id) != parseInt(that.data.fileInfo.user_id)) {
                that.setData({
                  //只修改一个值，其他不变，下载量+1
                  ['fileInfo.down_num']: parseInt(that.data.fileInfo.down_num) + 1,
                })
                //修改全局变量中的金币数
                getApp().globalData.userInfo.gold_all_num -= that.data.fileInfo.gold_num
              }

            }
          })

        }
      })
    } else {
      that.setData({
        pay_gold: false,
      })
      wx.showModal({
        title: '温馨提示',
        content: '【金币不足】您可以尝试上传文件设置金币，通过其他人下载文件，您将获得金币哦~',
        showCancel: false
      })
    }
  },

  //页面初始化
  onLoad: function (options) {
    wx.showLoading()
    var that = this
    console.log(options)
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/get_one_file',
      method: "POST", //请求方式
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        file_id: parseInt(options.id),
        have_read: options.have_read,
        gold_id: options.gold_id,
      },
      success(res) {
        console.log(res.data.data)
        that.setData({
          fileInfo: res.data.data,
        })
        
        wx.hideLoading()
      }
    })
  },

  //点赞
  giveLike: function () {
    var that = this
    
    if(getApp().globalData.userInfo.user_id!=undefined){
      wx.request({
        url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/like',
        method: "POST", //请求方式
        header: {
          'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
        },
        data: {
          file_id: parseInt(that.data.fileInfo.file_id),
        },
        success(res) {
          console.log(res)
          that.setData({
            //只修改一个值，其他不变，点赞数+1
            ['fileInfo.like_num']: parseInt(that.data.fileInfo.like_num) + 1,
            zan: false,
          })
        }
      })
    }else{
      wx.showModal({
        title: '提示!',
        content: '请授权后再使用小程序',
        showCancel: false,
        success(res) { 
          wx.navigateTo({
            url: '/pages/login/login'
          })
        }
      })
    }

  },

  //取消赞
  cancelLike: function () {
    var that = this
    console.log(that.data.fileInfo.like_num)
    wx.request({
      url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/cancel_like',
      method: "POST", //请求方式
      header: {
        'content-type': 'application/x-www-form-urlencoded' //POST提交数据方式
      },
      data: {
        file_id: parseInt(that.data.fileInfo.file_id),
      },
      success(res) {
        console.log(res)
        that.setData({
          //只修改一个值，其他不变，点赞数-1
          ['fileInfo.like_num']: parseInt(that.data.fileInfo.like_num) - 1,
          zan: true,
        })
      }
    })
  },

  //在线预览
  openFile: function () {
    var that = this

    if(getApp().globalData.userInfo.user_id!=undefined){
      wx.showLoading({
        title: '加载中...',
      })
      //根据https路径可以获得http格式的路径(指定文件下载后缓存的路径 (本地路径)),根据这个路径可以预览
      wx.downloadFile({
        url: 'https://www.cerywxr.com/treehole/index.php/Home/sharing/down_file?file_id=' + that.data.fileInfo.file_id,
        success(res) {
          console.log(res)
          that.setData({
            httpfile: res.tempFilePath
          })
          //获取文件后缀
          const houzhui = res.tempFilePath.match(/\.[^.]+?$/)[0]; //文件后缀，不知道这串代码什么意思
          console.log(houzhui)
          if (houzhui == ".bin" || houzhui == ".unknown") {
            wx.hideLoading()
            wx.showModal({
              title: '温馨提示',
              content: '文件格式不支持预览',
              showCancel: false,
            })
          } else {
            //预览文件
            wx.openDocument({
              filePath: that.data.httpfile,
              showMenu: true,
              success: res => {
                wx.hideLoading()
                //console.log(res)
              },
              fail: err => {
                console.log(err);
                console.log(that.data.httpfile)
                wx.hideLoading()
                wx.showModal({
                  title: '温馨提示',
                  content: '文件格式不支持预览',
                  showCancel: false,
                })
              }
            })
          }
        },
        fail: (err) => {
          console.log('读取失败', err)
        }
      })
    }else{
      wx.showModal({
        title: '提示!',
        content: '请授权后再使用小程序',
        showCancel: false,
        success(res) { 
          wx.navigateTo({
            url: '/pages/login/login'
          })
        }
      })
    }
  },

   //跳转去举报页面
   toreport:function(){
    var that = this
    wx.navigateTo({
      url: '/pages/report/report?reported_user_id='+that.data.fileInfo.user_id+'&xx_id='+that.data.fileInfo.file_id+'&xx_content='+that.data.fileInfo.file_name+'&type=2',
    })
  },





})